<template>
	<view class="app">
		<view class="price-box">
			<text>支付金额</text>
			<text class="price">{{money}}</text>
		</view>

		<view class="pay-type-list">

			<view class="type-item b-b" @click="changePayType(item)" v-for="(item,index) in payTypeList" :key="index">
				<!-- <text class="icon yticon icon-weixinzhifu"></text> -->
				<image style="width: 80rpx;height: 80rpx;margin-right: 20rpx;" :src="$fun.imgUrl(item.image)" mode=""></image>
				<view class="con">
					<text class="tit">{{item.name}}</text>
					<text>{{item.money}}</text>
				</view>
				<label class="radio">
					<radio :value="item.code" color="#0066FF" :checked='payType == item.code' />
					</radio>
				</label>
			</view>
		</view>

		<text class="mix-btn" @click="handleModel">确认支付</text>
		<pay-board ref="payJP" :showBoard="showBoard" :priceText="money+''" @complete="complete" @closeBorad="handleModel"></pay-board>
	</view>
</template>

<script>
	import PayBoard from '@/components/xiaojia-keyBoard/xiaojia-keyBoard.vue'
	export default {
		components: {
			PayBoard
		},
		data() {
			return {
				payType: "",
				orderInfo: {},
				oid: '',
				// 支付方式
				payTypeList: [],
				showBoard: false,
				payMoney: '',
				money: 0
			};
		},
		computed: {

		},
		onLoad(options) {
			this.oid = options.oid
			this.getorder(this.oid)
		},

		methods: {
			handleModel() {
				//console.log(this.payType)
				if (this.payType == 'wechat'||this.payType == 'alipay') {
					this.confirm()
				} else {
					this.showBoard = !this.showBoard
				}
			},
			complete(pass) {
				this.$fun.ajax.post('user/setPay',{
					paypwd: pass
				}).then(res => {
					this.$refs.payJP.password = ''
					if (res.code == 1) {
						this.showBoard = false
						this.confirm()
					}
				})
			},
			// 获取订单
			getorder(oid) {
				this.$fun.ajax.post('/order/orderpay',{
					oid
				}).then(res => {
					if (res.code == 1) {
						this.payTypeList = res.data.pay
						this.payType = res.data.pay[0].code
						this.payMoney = res.data.pay[0].money
						this.money = res.data.money
					}
				})
			},
			//选择支付方式
			changePayType(item) {
				this.payType = item.code;
				console.log(this.payType)
				this.payMoney = item.money;
			},
			payPassword() {

			},
			wxpay(data) {
				var vm = this
				WeixinJSBridge.invoke(
					'getBrandWCPayRequest', { // 下面参数内容都是后台返回的
						'appId': data.appId, // 公众号名称，由商户传入
						'timeStamp': data.timeStamp, // 时间戳
						'nonceStr': data.nonceStr, // 随机串
						'package': data.package, // 预支付id
						'signType': data.signType, // 微信签名方式
						'paySign': data.paySign // 微信签名
					},
					function(res) {
						// 使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回ok，但并不保证它绝对可靠。
						if (res.err_msg === 'get_brand_wcpay_request:ok') {
							vm.$fun.msg('支付成功')
							uni.redirectTo({
								url: '/pageB/paySuccess?status=1'
							})
						} else {
							vm.$fun.msg('支付失败')
						}
					}
				)
			},
			//确认支付
			async confirm() {
				let vm = this;
				await this.$fun.ajax.post('/order/pay',{
					oid: this.oid,
					type: this.payType
				}).then(res => {
					let data = res.data
					if (res.code == 1) {
						if (this.payType == 'wechat') {
							if (typeof WeixinJSBridge === 'undefined') { // 微信浏览器内置对象。参考微信官方文档
								if (document.addEventListener) {
									document.addEventListener('WeixinJSBridgeReady', vm.wxpay(data), false)
								} else if (document.attachEvent) {
									document.attachEvent('WeixinJSBridgeReady', vm.wxpay(data))
									document.attachEvent('onWeixinJSBridgeReady', vm.wxpay(data))
								}
							} else{
								vm.wxpay(data)
							}
							return
						}else  if (this.payType == 'alipay') {
								uni.requestPayment({
									//服务提供商 通过uni.getProvider获取
									provider: 'alipay',
									//后台返回的订单数据
									orderInfo: res.data,
									//调用成功的回调
									success(res) {
										this.$fun.msg('支付成功')
										uni.redirectTo({
											url: '/pageB/paySuccess?status=1'
										})
									},
									//调用失败的回调
									fail(err) {
										console.log(err)
										console.log('fail:' + err);
									}
								})
								return
							}
						uni.redirectTo({
							url: '/pageB/paySuccess?status=1'
						})
					} else {
						uni.showToast({
							title: res.data.msg,
							icon: "none"
						})
					}
				})
			},
		}
	}
</script>

<style lang='scss'>
	.app {
		width: 100%;
	}

	.price-box {
		background-color: #fff;
		height: 265upx;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		font-size: 28upx;
		color: #909399;

		.price {
			font-size: 50upx;
			color: #303133;
			margin-top: 12upx;

			&:before {
				content: '￥';
				font-size: 40upx;
			}
		}
	}

	.pay-type-list {
		margin-top: 20upx;
		background-color: #fff;
		padding-left: 60upx;

		.type-item {
			height: 120upx;
			padding: 20upx 0;
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding-right: 60upx;
			font-size: 30upx;
			position: relative;
		}

		.icon {
			width: 100upx;
			font-size: 52upx;
		}

		.icon-erjiye-yucunkuan {
			color: #fe8e2e;
		}

		.icon-weixinzhifu {
			color: #36cb59;
		}

		.icon-alipay {
			color: #01aaef;
		}

		.tit {
			font-size: $font-lg;
			color: $font-color-dark;
			margin-bottom: 4upx;
		}

		.con {
			flex: 1;
			display: flex;
			flex-direction: column;
			font-size: $font-sm;
			color: $font-color-light;
		}
	}

	.mix-btn {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 630upx;
		height: 80upx;
		margin: 80upx auto 30upx;
		font-size: $font-lg;
		color: #fff;
		background-color: $base-color;
		border-radius: 10upx;
		box-shadow: 1px 2px 5px rgba(219, 63, 96, 0.4);
	}
</style>
